package com.ben.chatroom.common.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.nio.charset.StandardCharsets;

/**
 * @author huangyijun
 */
public class JsonUtil {

  static GsonBuilder gb = new GsonBuilder();

  private static final Gson gson;

  static {
    gb.disableHtmlEscaping();
    gson = gb.create();
  }

  //Object对象转成JSON字符串后，进一步转成字节数组
  public static byte[] object2JsonBytes(Object obj) {

    return pojoToJson(obj).getBytes(StandardCharsets.UTF_8);
  }

  public static String pojoToJson(Object obj) {
    return gson.toJson(obj);
  }

  public static <T> T jsobToPojo(String json, Class<T> clazz) {
    return gson.fromJson(json, clazz);
  }

}
